package com.lantosec.hmrp.modules.revisit.controller;


import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.modules.revisit.entity.HisPatientInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.revisit.entity.RevisitNote;
import com.lantosec.hmrp.modules.revisit.service.IRevisitNoteService;
import com.lantosec.hmrp.modules.revisit.vo.RevisitNoteVO;

import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RestController;
import com.lantosec.hmrp.core.mvc.BaseController;

/**
 *
 * 回访及复核记录 控制器
 *
 * @author AutoCode
 * @since 2020-05-06
 */
@Api(value = "回访及复核记录控制器", tags = "回访及复核记录控制器")
@RestController
@RequestMapping("/revisit/app/note")
@Log4j2
public class RevisitNoteController extends BaseController {
    @Autowired
    private IRevisitNoteService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<RevisitNoteVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<Page<RevisitNoteVO>> page(@ApiParam(value = "分页对象", required = true) PageVO<RevisitNoteVO> page,
                                            @ApiParam(value = "查询月份", required = true) @RequestParam(name = "date") String date){
        service.getAll(page,date);
        return Result.ok(page);
    }


    @PostMapping("create")
    @ApiOperation(value = "开始回访")
    public Result<Integer> create(@ApiParam(value = "回访及复核记录VO对象", required = true) @RequestBody @Validated RevisitNoteVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        RevisitNote entity = vo.convertToEntity();
        service.startRevisit(entity);
        return Result.ok(entity.getRevisitNoteId());
    }

    @PostMapping("review")
    @ApiOperation(value = "回访复查")
    public Result<Integer> review(@ApiParam(value = "回访及复核记录VO对象", required = true) @RequestBody RevisitNoteVO vo){
        RevisitNote entity = vo.convertToEntity();
        service.review(entity);
        return Result.ok();
    }

    @GetMapping("getHisPatientInfo")
    @ApiOperation(value = "患者回访记录列表")
    public Result<List<HisPatientInfo>> getHisPatientInfo(@ApiParam(value = "查询月份", required = true) @RequestParam(name = "date") String date){
        List<HisPatientInfo> hisPatientInfoList = service.getHisPatientInfo(date);
        return Result.ok(hisPatientInfoList);
    }

    @GetMapping("get/{revistTaskId}")
    @ApiOperation(value = "查看患者回访记录")
    public Result<List<RevisitNote>> get(@ApiParam(value = "任务id", required = true) @PathVariable(name="revistTaskId") Integer revistTaskId){
        List<RevisitNote> revisitNoteList = service.list(new QueryWrapper<RevisitNote>().eq(RevisitNote.ColName.revisit_task_id.toString(),revistTaskId)
                                                                                        .orderByDesc(RevisitNote.ColName.review_time.toString()));
        return Result.ok(revisitNoteList);
    }
}
